package com.efast.dmom.aps.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.dmom.aps.bean.ResultBean;
import com.efast.dmom.aps.bean.Supplier;
import com.efast.dmom.aps.entity.SupplierEntity;
import com.efast.dmom.aps.entity.base.IModificationFlags;
import com.efast.dmom.aps.service.SupplierService;

/**
 * 
 * @author WJ
 * @date modify 2020/2/20
 *
 */
@Controller
@RequestMapping(value = "/supplier")
public class SupplierController {

	@Autowired
	private SupplierService supplierService;

	@RequestMapping(value = "/operateBySupplier")
	public @ResponseBody ResultBean operateBySupplier(@RequestBody Supplier supplier) {
		
		SupplierEntity entity = supplier.getInnerData();
		// Determine the new
		if (entity.getModificationFlag() == IModificationFlags.NEW) {
			return supplierService.save(supplier);
			// Judge update
		} else if (entity.getModificationFlag() == IModificationFlags.MODIFIED) {
			return supplierService.update(supplier);
			// Judge to delete
		} else if (entity.getModificationFlag() == IModificationFlags.DELETED) {
			return supplierService.delete(supplier);
		}
		return null;
	}

	// get supplier by id
	@RequestMapping(value = "/getSupplierById/{id}")
	public @ResponseBody Supplier getSupplierById(@PathVariable long id) {
		return supplierService.getById(id);
	}

}
